
#include "MetaDataItem.hpp"
#include "PropertyDataItem.hpp"
#include <QFileInfo>

#include <vtkTextProperty.h>
#include <vtkProperty.h>
#include <vtkVertex.h>

namespace NSDatax {

	MetaDataItem::MetaDataItem() :
		NSCore::AbstractDataItem()
	{
		init();
	}

	MetaDataItem::MetaDataItem(QTreeWidget* tree) :
		NSCore::AbstractDataItem(tree)
	{
		init();
	}

	MetaDataItem::MetaDataItem(QTreeWidgetItem* view) :
		NSCore::AbstractDataItem(view)
	{
		init();
	}

	MetaDataItem::~MetaDataItem() {

	}

	void MetaDataItem::init() {
		setData(TreeField::FieldType, Qt::UserRole, TreeType::TypeMeta);
		setData(TreeField::FieldRemovable, Qt::UserRole, true);

		m_actor = Sp(vtkActor)::New();
		m_assembly = Sp(vtkAssembly)::New();
		m_polydata = Sp(vtkUnstructuredGrid)::New();
		m_polydata_mapper = Sp(vtkDataSetMapper)::New();
		m_meta_label = Sp(vtkBillboardTextActor3D)::New();

		const auto& label_prop = m_meta_label->GetTextProperty();
		label_prop->SetFontFamilyToArial();
		label_prop->SetFontSize(18);
		label_prop->SetBold(false);
		label_prop->SetItalic(false);
		label_prop->SetColor(m_label_color);
		label_prop->SetFrame(false);
		m_meta_label->SetPickable(false);

		addFeature(m_assembly);
		addFeature(m_meta_label);
	}

	void MetaDataItem::setMetaInput(Sp(vtkUnstructuredGrid) ugrid) {
		m_polydata = ugrid;
		const double unit = -66000;
		m_polydata_mapper->SetResolveCoincidentTopologyToPolygonOffset();
		m_polydata_mapper->SetRelativeCoincidentTopologyLineOffsetParameters(0, unit);
		m_polydata_mapper->SetRelativeCoincidentTopologyPolygonOffsetParameters(0, unit);
		m_polydata_mapper->SetRelativeCoincidentTopologyPointOffsetParameter(unit);
		m_polydata_mapper->SetScalarVisibility(false);
		m_polydata_mapper->SetInputData(ugrid);
		m_actor->SetMapper(m_polydata_mapper);
		m_actor->GetProperty()->SetEdgeColor(0, 0, 0);
		m_actor->GetProperty()->SetEdgeVisibility(false);
		m_actor->GetProperty()->SetColor(m_meta_color);
		m_actor->GetProperty()->SetLineWidth(m_linewidth);
		m_actor->GetProperty()->SetPointSize(m_pointsize);
		/*Settings*/
		m_actor->GetProperty()->SetAmbient(1.0);
		m_actor->GetProperty()->SetDiffuse(1.0);
		m_actor->GetProperty()->SetSpecular(5.0);
		m_actor->GetProperty()->SetSpecularPower(100);

		m_actor->SetPickable(false);
		m_assembly->SetPickable(false);
		m_assembly->AddPart(m_actor);

	}

	void MetaDataItem::setDisplay(QString display, double* postion) {
		if (!m_assembly && m_actor)return;
		m_display = display;
		m_meta_label->SetInput(display.toStdString().c_str());
		if (postion != nullptr) {
			m_meta_label->SetPosition(postion);
		}
		else {
			double bounds[6], center[3];
			m_assembly->GetBounds(bounds);
			center[0] = (bounds[0] + bounds[1]) / 2.0;
			center[1] = (bounds[2] + bounds[3]) / 2.0;
			center[2] = (bounds[4] + bounds[5]) / 2.0;
			m_meta_label->SetPosition(center);
		}
	}

	void MetaDataItem::getPropertyTree(QTreeWidget* p_tree) {
		const auto& top = new PropertyDataItem(p_tree);
		top->setIcon(0, icon(0));
		top->setText(0, text(0));
		top->setExpanded(true);

	}

}

